import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class text extends JFrame {

    private static final long serialVersionUID = 1L;
    public text(){
        super("绘图测试");
        this.setSize(300, 400);
        this.setBackground(Color.green);
        this.setLocation(300, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel panel=new Mypanel();//创建Mypanel的对象，Mypanel继承了JPanel并重写了paint()方法
        panel.setBackground(Color.blue);//设置面本的背景颜色
        this.addKeyListener(panel);//为JFrame类型的类添加键盘监听
        this.add(panel);
        this.setVisible(true);
    }
    public static void main(String[] args)  {
        new text();
    }
}

class Mypanel extends JPanel implements KeyListener{
    private static final long serialVersionUID = 1L;
    int x=80,y=100;
    //重写paint()方法
    public void paint(Graphics e){
        super.paint(e);
        Graphics2D g=(Graphics2D)e;
        g.setColor(Color.green);
        g.setFont(new Font("宋体",Font.BOLD,50));
        g.drawString("大家好", 60, 70);
        g.setStroke(new BasicStroke(3.0f));
        g.drawOval(x, y, 150, 150);
        g.drawOval(x+30, y+40, 15, 10);
        g.drawOval(x+115, y+40, 15, 10);
        g.drawRect(x+70, y+70, 10, 10);
        g.drawOval(x+70, y+100, 10, 6);
    }
    @Override
    public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
    }
    @Override
    public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
        int m=e.getKeyCode();
        if(m==KeyEvent.VK_DOWN){
            y+=10;
            System.out.println("shjsdj");
            this.repaint();
        }if(m==KeyEvent.VK_LEFT){
            x-=10;
        }if(m==KeyEvent.VK_RIGHT){
            x+=10;
        }if(m==KeyEvent.VK_UP){
            y-=10;
        }
        this.repaint();//调用repaint()方法
        System.out.println("y="+y);
    }
    @Override
    public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
    }
}